package com.panda.common.util;


import java.util.ArrayList;
import java.util.List;

public class Pager {
	
	/** 默认页面大小 */
	public static final int PAGESIZE = 20;

	/** 当前页数 */
	private int pageNo;
	/** 总页数 */
	private int totalPage;
	/** 总记录数 */
	private int totalRecord;
	/** 每页数目 */
	private int pageSize = PAGESIZE;
	
	private List<Integer> pageNoList;
	
	@SuppressWarnings("unused")
    private boolean hasFirst;
	@SuppressWarnings("unused")
    private boolean hasNext;
	@SuppressWarnings("unused")
    private boolean hasPrevious;
	@SuppressWarnings("unused")
    private boolean hasLast;
	
	/** 开始记录数 */
	private int startRecord;
	@SuppressWarnings("unchecked")
    /** 当前页数据列表 */
	private List data;
	
	public Pager() {
		super();
	}
	
	public Pager(int pageNo, int totalRecord) {
		this.totalRecord = totalRecord;
		if (totalRecord <= 0)
			totalPage = 0;
		else
			totalPage = (totalRecord-1)/pageSize +1;
		if (pageNo<1) {
			this.pageNo = 1;
		}else if(pageNo > totalPage){
			this.pageNo = totalPage;
		}else {
			this.pageNo = pageNo;
		}
	}
	
	public Pager(int pageNo, int totalRecord,int pagesize) {
		this.pageSize = pagesize;
		this.totalRecord = totalRecord;
		if (totalRecord <= 0)
			totalPage = 0;
		else
			totalPage = (totalRecord-1)/pageSize +1;
		if (pageNo<1) {
			this.pageNo = 1;
		}else if(pageNo > totalPage){
			this.pageNo = totalPage;
		}else {
			this.pageNo = pageNo;
		}
	}
	
	@SuppressWarnings("unchecked")
    public Pager(int pageNo, int totalRecord,int pagesize,List pagedata) {
		this.pageSize = pagesize;
		this.totalRecord = totalRecord;
		if (totalRecord <= 0)
			totalPage = 0;
		else
			totalPage = (totalRecord-1)/pageSize +1;
		if (pageNo<1) {
			this.pageNo = 1;
		}else if(pageNo > totalPage){
			this.pageNo = totalPage;
		}else {
			this.pageNo = pageNo;
		}
		this.data = pagedata;
		
		/* add page no list */
		List pageNoList = new ArrayList<Integer>();
		int page = pageNo;
		for (int i = page - 4; i < page + 4 + ((page - 4)<=0?-(page-5):0); i++){
            if (i > 0 && i <= totalPage){
            	pageNoList.add(i);
            }
        }
		this.pageNoList = pageNoList;
	}
	
	public static int getStartIndexOfPage(int pageno, int pagesize) {
        return (pageno - 1) * pagesize;
    }
	
	public static int getEndIndexOfPage(int pageNo, int pageSize, int recordCount) {

		int startIndex = getStartIndexOfPage(pageNo, pageSize);
		
		if (startIndex < recordCount) {
			
			int endIndex = startIndex + pageSize;
			
			if (endIndex >= recordCount)
				
				endIndex = recordCount;
			
			return endIndex;
			
		} else
			
			return startIndex;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public boolean isHasFirst() {
		return this.pageNo>1?true:false;
	}

	public void setHasFirst(boolean hasFirst) {
		this.hasFirst = hasFirst;
	}

	public boolean isHasNext() {
		return this.pageNo<this.totalPage?true:false;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public boolean isHasPrevious() {
		return this.isHasFirst();
	}

	public void setHasPrevious(boolean hasPrevious) {
		this.hasPrevious = hasPrevious;
	}

	public boolean isHasLast() {
		return this.isHasNext();
	}

	public void setHasLast(boolean hasLast) {
		this.hasLast = hasLast;
	}

	public int getStartRecord() {
		return startRecord;
	}

	public void setStartRecord(int startRecord) {
		this.startRecord = startRecord;
	}

	@SuppressWarnings("unchecked")
    public List getData() {
		return data;
	}

	@SuppressWarnings("unchecked")
    public void setData(List pagedata) {
		this.data = pagedata;
	}

	public List getPageNoList() {
		return pageNoList;
	}

	public void setPageNoList(List pageNoList) {
		this.pageNoList = pageNoList;
	}
	
}

